package IOBaseTest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yexuan on 2018/3/15.
 * 将d盘文件路径存入文件中
 */
public class FileToListDemo {
    public static void main(String[] args){
        File f = new File("D:\\soft\\project\\CollectionTest");
        List<File> fileList = new ArrayList<File>();
        fileToList(f,fileList);

        File f2 = new File("D:\\文档\\allFiles.txt");
        listToFile(fileList,f2.toString());

    }

    public static void fileToList(File file, List<File> flist){
        File[] files = file.listFiles();
        for (File f: files) {
            if(f.isDirectory())
                fileToList(f, flist);
            else
                flist.add(f);
        }
    }

    public static void listToFile(List<File> flist,String file){
        BufferedWriter bw = null;
        try{
            bw = new BufferedWriter(new FileWriter(file));
            for (File f: flist) {
                bw.write(f.getAbsolutePath());
                bw.newLine();
                bw.flush();
            }
        }catch (IOException e){
            System.out.println(e);;
        }finally {
            if(bw!=null)
                try {
                    bw.close();
                }catch (IOException e){

                }
        }
    }
}
